Requires DLL-Tools
Requires SDL
Requires OpenGL
Requires Tools
Requires Rnd

Only Forth Definitions

\ I'll need to add a platform-check here for portability...
\ ------------ win32 stuff
: SDL_WMInfo->Window
  CELL+ @ ;

: get-proc-address ( addr c - addr )
   z$ wglGetProcAddress ;

: set-window-text
  SetWindowText drop ;

: set-foreground-window
  SetForegroundWindow DROP ;
\ ----------- end win32 stuff

: seki-ABORT ( addr c -- )
  -1 -rot PAD PLACE  s"  " pad append  SDL_GetError ZCOUNT PAD APPEND PAD COUNT ?ABORT ;

: close-display ( -- )
  display? if
    SDL_QuitSubSystem( SDL_INIT_VIDEO )
    false to display?
  then ;

: desktop
   desktop-w desktop-h ;

: resolution
   SDL_GetVideoInfo 3 cells + 2@ swap ;

: .driver
   ." SDL version " SDL_Linked_Version dup c@ . 1 + dup c@ . 1 + c@ . ;

: .rect
   h@+ . h@+ . h@+ . h@ . ;

: .modes
   cr ." SDL "
   0 SDL_FULLSCREEN SDL_OPENGL or SDL_ListModes
   begin
   @+ ?dup while
      cr .rect
   repeat drop ;

: init-video-driver
  SDL_Init( SDL_INIT_VIDEO ) if
     s" Unable to initialize SDL: " seki-ABORT then
  resolution to desktop-h to desktop-w ;

: caption! ( addr c -- )
  2dup caption place
  z$ seki-window swap set-window-text ;

: set-videomode ( w h fullscreen -- )
  display?  if close-display  then  init-video-driver
  to fullscreen? to screen-h to screen-w
  fullscreen? if SDL_FULLSCREEN else SDL_RESIZABLE then SDL_OPENGL or >r
  SDL_GL_SetAttribute( SDL_GL_RED_SIZE  8  )
  SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE  8  )
  SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE  8  )
  SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE  8  )
  SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE  8  )
  SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE  24   )
  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER 1 )
\  SDL_GL_SetAttribute( SDL_GL_SWAP_CONTROL 1 )
\  SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL 1 )
  SDL_SetVideoMode( screen-w screen-h 32 r> ) 0= if false to display? s" Error: Could not set video mode." seki-abort then
  true to display?
  \ GetForegroundWindow to seki-window
  0 z" SDL_app" FindWindow to seki-window
  -1 rnd pad !  pad 4 caption!
  0 pad 4 z$ FindWindow to seki-window
  s" Window" caption!

  ; \ PAD SDL_GetWMInfo PAD SDL_WMInfo->Window to seki-window  ;

: set-display ( w h fullscreen -- )
  init-video-driver
  ['] set-videomode catch if
     screen-w 2/ screen-h 2/ false ['] set-videomode catch if
        s" Unable to set video mode: " seki-ABORT
     then
  then
  true SDL_ShowCursor ;

0 value frame-timer
: seki-end-frame ( -- )
   display? 0= ?exit
   SDL_GL_SwapBuffers
   ." " \ to allow ide to break
   ;

get-order
Requires Seki\seki-sdl-surfaces
set-order

: seki-cleanup
  \ cr ." Deleting stray surfaces..."
  \ seki-sdl-cleanup-surfaces
  cr ." Closing the display..."
  close-display ;

:prune  seki-cleanup ;
:onexit   seki-cleanup ;
